<template>
  <div class="student-home">
    <!-- 欢迎区域 -->
    <div class="welcome-section">
      <div class="welcome-content">
        <h1 class="welcome-title">
          欢迎回来，{{ userStore.userInfo.student?.name || '同学' }}！
        </h1>
        <p class="welcome-subtitle">
          今天是 {{ formatDate(new Date(), 'YYYY年MM月DD日') }}，
          发现更多精彩活动，丰富你的校园生活
        </p>
      </div>
      <div class="quick-actions">
        <el-button type="primary" @click="$router.push('/student/activities')">
          <el-icon><Calendar /></el-icon>
          浏览活动
        </el-button>
        <el-button @click="$router.push('/student/registrations')">
          <el-icon><List /></el-icon>
          我的报名
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon pending">
              <el-icon><Clock /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ stats.pendingRegistrations }}</div>
              <div class="stat-label">待审核报名</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon approved">
              <el-icon><Check /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ stats.approvedRegistrations }}</div>
              <div class="stat-label">已通过报名</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon activities">
              <el-icon><Calendar /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ stats.availableActivities }}</div>
              <div class="stat-label">可报名活动</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon feedback">
              <el-icon><ChatDotRound /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ stats.pendingFeedback }}</div>
              <div class="stat-label">待评价活动</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 主要内容区域 -->
    <el-row :gutter="20">
      <!-- 最新活动 -->
      <el-col :xs="24" :lg="12">
        <el-card class="content-card">
          <template #header>
            <div class="card-header">
              <span class="card-title">
                <el-icon><Calendar /></el-icon>
                最新活动
              </span>
              <el-link type="primary" @click="$router.push('/student/activities')">
                查看更多
              </el-link>
            </div>
          </template>
          
          <div v-if="loading.activities" class="loading-container">
            <el-skeleton :rows="3" animated />
          </div>
          
          <div v-else-if="latestActivities.length === 0" class="empty-container">
            <el-empty description="暂无最新活动" />
          </div>
          
          <div v-else class="activities-list">
            <div 
              v-for="activity in latestActivities" 
              :key="activity.id"
              class="activity-item"
              @click="$router.push(`/student/activities/${activity.id}`)"
            >
              <div class="activity-header">
                <h4 class="activity-title">{{ activity.title }}</h4>
                <el-tag :type="getActivityStatus(activity).type" size="small">
                  {{ getActivityStatus(activity).text }}
                </el-tag>
              </div>
              <div class="activity-meta">
                <span class="meta-item">
                  <el-icon><Clock /></el-icon>
                  {{ formatDate(activity.startTime, 'MM-DD HH:mm') }}
                </span>
                <span class="meta-item">
                  <el-icon><Location /></el-icon>
                  {{ activity.location }}
                </span>
              </div>
              <p class="activity-description">{{ activity.description }}</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <!-- 我的报名状态 -->
      <el-col :xs="24" :lg="12">
        <el-card class="content-card">
          <template #header>
            <div class="card-header">
              <span class="card-title">
                <el-icon><List /></el-icon>
                我的报名
              </span>
              <el-link type="primary" @click="$router.push('/student/registrations')">
                查看全部
              </el-link>
            </div>
          </template>
          
          <div v-if="loading.registrations" class="loading-container">
            <el-skeleton :rows="3" animated />
          </div>
          
          <div v-else-if="recentRegistrations.length === 0" class="empty-container">
            <el-empty description="暂无报名记录" />
          </div>
          
          <div v-else class="registrations-list">
            <div 
              v-for="registration in recentRegistrations" 
              :key="registration.id"
              class="registration-item"
            >
              <div class="registration-header">
                <h4 class="registration-title">{{ registration.activity.title }}</h4>
                <el-tag :type="getRegistrationStatus(registration.status).type" size="small">
                  {{ getRegistrationStatus(registration.status).text }}
                </el-tag>
              </div>
              <div class="registration-meta">
                <span class="meta-item">
                  <el-icon><Clock /></el-icon>
                  报名时间：{{ formatDate(registration.createdAt, 'MM-DD HH:mm') }}
                </span>
              </div>
              <div v-if="registration.status === 'REJECTED' && registration.reason" class="rejection-reason">
                <span class="reason-label">拒绝原因：</span>
                <span class="reason-text">{{ registration.reason }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 即将开始的活动 -->
    <el-card class="content-card" v-if="upcomingActivities.length > 0">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            <el-icon><Bell /></el-icon>
            即将开始的活动
          </span>
        </div>
      </template>
      
      <div class="upcoming-activities">
        <div 
          v-for="activity in upcomingActivities" 
          :key="activity.id"
          class="upcoming-item"
        >
          <div class="upcoming-time">
            <div class="time-day">{{ formatDate(activity.startTime, 'DD') }}</div>
            <div class="time-month">{{ formatDate(activity.startTime, 'MM月') }}</div>
          </div>
          <div class="upcoming-content">
            <h4 class="upcoming-title">{{ activity.title }}</h4>
            <div class="upcoming-meta">
              <span class="meta-item">
                <el-icon><Clock /></el-icon>
                {{ formatDate(activity.startTime, 'HH:mm') }} - {{ formatDate(activity.endTime, 'HH:mm') }}
              </span>
              <span class="meta-item">
                <el-icon><Location /></el-icon>
                {{ activity.location }}
              </span>
            </div>
          </div>
          <div class="upcoming-actions">
            <el-button size="small" @click="$router.push(`/student/activities/${activity.id}`)">
              查看详情
            </el-button>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { formatDate, getActivityStatus, getRegistrationStatus } from '@/utils'
import api from '@/utils/api'
import {
  Calendar,
  List,
  Clock,
  Check,
  ChatDotRound,
  Location,
  Bell
} from '@element-plus/icons-vue'

const userStore = useUserStore()

const loading = reactive({
  activities: false,
  registrations: false
})

const stats = reactive({
  pendingRegistrations: 0,
  approvedRegistrations: 0,
  availableActivities: 0,
  pendingFeedback: 0
})

const latestActivities = ref([])
const recentRegistrations = ref([])
const upcomingActivities = ref([])

const loadStats = async () => {
  try {
    const response = await api.get('/students/dashboard/stats')
    Object.assign(stats, response.data.data)
  } catch (error) {
    console.error('加载统计数据失败:', error)
  }
}

const loadLatestActivities = async () => {
  try {
    loading.activities = true
    const response = await api.get('/activities/latest?limit=5')
    latestActivities.value = response.data.data || []
  } catch (error) {
    console.error('加载最新活动失败:', error)
  } finally {
    loading.activities = false
  }
}

const loadRecentRegistrations = async () => {
  try {
    loading.registrations = true
    const response = await api.get('/registrations/recent?limit=5')
    recentRegistrations.value = response.data.data || []
  } catch (error) {
    console.error('加载最近报名失败:', error)
  } finally {
    loading.registrations = false
  }
}

const loadUpcomingActivities = async () => {
  try {
    const response = await api.get('/students/upcoming-activities')
    upcomingActivities.value = response.data.data || []
  } catch (error) {
    console.error('加载即将开始的活动失败:', error)
  }
}

onMounted(() => {
  loadStats()
  loadLatestActivities()
  loadRecentRegistrations()
  loadUpcomingActivities()
})
</script>

<style scoped>
.student-home {
  max-width: 1200px;
  margin: 0 auto;
}

/* 欢迎区域 */
.welcome-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px;
  border-radius: 12px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-content {
  flex: 1;
}

.welcome-title {
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.welcome-subtitle {
  opacity: 0.9;
  font-size: 14px;
}

.quick-actions {
  display: flex;
  gap: 12px;
}

/* 统计卡片 */
.stats-section {
  margin-bottom: 20px;
}

.stat-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
}

.stat-icon.pending {
  background: #e6a23c;
}

.stat-icon.approved {
  background: #67c23a;
}

.stat-icon.activities {
  background: #409eff;
}

.stat-icon.feedback {
  background: #909399;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #606266;
}

/* 内容卡片 */
.content-card {
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #303133;
}

.loading-container,
.empty-container {
  padding: 20px;
}

/* 活动列表 */
.activities-list {
  max-height: 400px;
  overflow-y: auto;
}

.activity-item {
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.3s;
}

.activity-item:hover {
  background-color: #f5f7fa;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.activity-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  flex: 1;
  margin-right: 12px;
}

.activity-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 8px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #606266;
}

.activity-description {
  color: #909399;
  font-size: 14px;
  line-height: 1.4;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 报名列表 */
.registrations-list {
  max-height: 400px;
  overflow-y: auto;
}

.registration-item {
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.registration-item:last-child {
  border-bottom: none;
}

.registration-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.registration-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  flex: 1;
  margin-right: 12px;
}

.registration-meta {
  margin-bottom: 8px;
}

.rejection-reason {
  background: #fef0f0;
  padding: 8px 12px;
  border-radius: 4px;
  border-left: 3px solid #f56c6c;
}

.reason-label {
  font-weight: 600;
  color: #f56c6c;
}

.reason-text {
  color: #606266;
}

/* 即将开始的活动 */
.upcoming-activities {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.upcoming-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #409eff;
}

.upcoming-time {
  text-align: center;
  min-width: 60px;
}

.time-day {
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
  line-height: 1;
}

.time-month {
  font-size: 12px;
  color: #606266;
}

.upcoming-content {
  flex: 1;
}

.upcoming-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
}

.upcoming-meta {
  display: flex;
  gap: 16px;
}

.upcoming-actions {
  display: flex;
  gap: 8px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-section {
    flex-direction: column;
    text-align: center;
    gap: 20px;
  }
  
  .welcome-title {
    font-size: 1.5rem;
  }
  
  .quick-actions {
    justify-content: center;
  }
  
  .stat-card {
    padding: 15px;
  }
  
  .stat-number {
    font-size: 20px;
  }
  
  .activity-header,
  .registration-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .activity-meta,
  .upcoming-meta {
    flex-direction: column;
    gap: 8px;
  }
  
  .upcoming-item {
    flex-direction: column;
    text-align: center;
  }
}
</style>